package com.yy.jhl.ui.manager.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.foamtrace.photopicker.ImageCaptureManager;
import com.foamtrace.photopicker.PhotoPickerActivity;
import com.foamtrace.photopicker.PhotoPreviewActivity;
import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.FileUtil;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.bean.LogisticsInfo;
import com.yy.jhl.bean.ProductInfo;
import com.yy.jhl.bean.SealBoxListInfo;
import com.yy.jhl.interfaces.CommonCallBack;
import com.yy.jhl.interfaces.LogisticsCallBack;
import com.yy.jhl.ui.main.activity.ClipImageActivity;
import com.yy.jhl.ui.manager.adapter.SealBoxAdapter;
import com.yy.jhl.ui.manager.contract.SealBoxListContract;
import com.yy.jhl.ui.manager.presenter.SealBoxListPresenter;
import com.yy.jhl.util.DataRequestUtil;
import com.yy.jhl.util.DialogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by libingn on 2017/12/11.
 */

public class SealBoxListActivity extends BaseActivity<SealBoxListPresenter> implements SealBoxListContract.View {
    @BindView(R.id.add_btn)
    Button addBtn;
    @BindView(R.id.no_data_parent)
    LinearLayout noDataParent;
    @BindView(R.id.list_view)
    ListView listView;
    @BindView(R.id.note_edit)
    EditText noteEdit;
    @BindView(R.id.again_btn)
    Button againBtn;
    @BindView(R.id.no_again_btn)
    Button noAgainBtn;
    @BindView(R.id.message_btn)
    Button messageBtn;
    @BindView(R.id.check_parent)
    LinearLayout checkParent;
    @BindView(R.id.check_hint_text)
    TextView checkHintText;
    SealBoxAdapter sealBoxAdapter;
    String status = "";//审核状态1、默认2、通过3、拒绝4、留言
    String shipStatus = "";
    String batchId = "";
    @BindView(R.id.chuku_btn)
    Button chukuBtn;
    //1 :供应商 2：采购商
    private int currentLoginType = 1;

    public static void startAction(Context mContext, String batchId, String orderId, String status,String shipStatus, LogisticsInfo logisticsInfo) {
        Intent intent = new Intent(mContext, SealBoxListActivity.class);
        intent.putExtra("batchId", batchId);
        intent.putExtra("orderId", orderId);
        intent.putExtra("status", status);
        intent.putExtra("shipStatus", shipStatus);
        intent.putExtra("logisticsInfo", logisticsInfo);
        mContext.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_seal_box_list;
    }

    @Override
    protected void onStart() {
        mPresenter.getList(batchId);
        super.onStart();
    }

    @Override
    public void initView() {
        currentLoginType = SPUtils.getSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE);
        shipStatus = getIntent().getStringExtra("shipStatus");
        status = getIntent().getStringExtra("status");
        batchId = getIntent().getStringExtra("batchId");
        addBtn.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
//                SealBoxAddActivity.startAction(mContext, "", batchId, "", null,1000);
                SealBoxAddWebViewActivity.startAction(mContext,"",batchId,1000);
            }
        });
        boolean isEdit = false;
        String title = "";
        ////审核状态1、默认2、通过3、拒绝4、留言
        //出库审核失败可对封箱进行编辑
        if ("7".equals(shipStatus) && currentLoginType == 1  && "3".equals(status)){
            isEdit = true;
            title = "封箱获取二维码";
            addBtn.setVisibility(View.VISIBLE);
            checkParent.setVisibility(View.GONE);
            checkHintText.setVisibility(View.GONE);
        } else if ("2".equals(status) && currentLoginType == 1 && ("5".equals(shipStatus) || "7".equals(shipStatus) )) {
            isEdit = true;
            title = "封箱获取二维码";
            addBtn.setVisibility(View.VISIBLE);
            checkParent.setVisibility(View.GONE);
            checkHintText.setVisibility(View.GONE);
        } else {
            isEdit = false;
            title = "封箱信息";
            addBtn.setVisibility(View.GONE);
            checkParent.setVisibility(View.GONE);
            checkHintText.setVisibility(View.GONE);
            checkHintText.setText("审核通过！    ");
            checkHintText.setTextColor(getResources().getColor(R.color.green_color));
        }
        sealBoxAdapter = new SealBoxAdapter(mContext, isEdit);
        listView.setAdapter(sealBoxAdapter);
        listView.setFocusable(false);
        showTitle(title, new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
//        if ("7".equals(shipStatus) && "2".equals(status)&& currentLoginType == 1){
//            chukuBtn.setVisibility(View.VISIBLE);
//        }else {
//            chukuBtn.setVisibility(View.GONE);
//        }
    }

    @Override
    public void getListSuccess(SealBoxListInfo info) {
        if (info.getData() != null && info.getData().size() > 0) {
            noDataParent.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
            sealBoxAdapter.addAllData(info.getData());
            sealBoxAdapter.setBatchId(batchId);
            StringUtil.setListViewHeightBasedOnChildren(listView);
        } else {
            noDataParent.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
        }
    }

    @Override
    public void getListFail() {

    }

    @OnClick({R.id.again_btn, R.id.no_again_btn, R.id.message_btn})
    public void onViewClicked(View view) {
        int status = 1;
        switch (view.getId()) {
            case R.id.again_btn:
                status = 2;
                break;
            case R.id.no_again_btn:
                status = 3;
                break;
            case R.id.message_btn:
                status = 4;
                break;
        }
        String remark = noteEdit.getText().toString().trim();
        if (StringUtil.isEmpty(remark)) {
            showShortToast("请输入备注信息！");
            return;
        }
        DataRequestUtil.getInstance(mContext).flowCheck(batchId, 5, status, remark, new CommonCallBack() {
            @Override
            public void getCallBack() {
                finish();
            }
        });
    }
//    @OnClick(R.id.chuku_btn)
//    public void onViewClicked() {
//        LogisticsInfo logisticsInfo = (LogisticsInfo) getIntent().getSerializableExtra("logisticsInfo");
//        OutboundAfterActivity.startAction(mContext, logisticsInfo);
//    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1000:
                    String is_finish = data.getStringExtra("is_finish");
                    if (!StringUtil.isEmpty(is_finish) && "1".equals(is_finish)){
                        DialogUtil.showEnsureDialogView(mContext, "您已将所有产品封箱，是否出库！", new CommonCallBack() {
                            @Override
                            public void getCallBack() {
                                DataRequestUtil.getInstance(mContext).getLogisticsInfo(batchId, new LogisticsCallBack() {
                                    @Override
                                    public void getCallBack(LogisticsInfo logisticsInfo) {
                                        OutboundAfterActivity.startAction(mContext, logisticsInfo);
                                        finish();
                                    }
                                });
//                                LogisticsInfo logisticsInfo = (LogisticsInfo) getIntent().getSerializableExtra("logisticsInfo");
                            }
                        });
                    }
                    break;

            }
        }
    }
}
